package com.example.spark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.spark.entity.Adviser;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.AdviserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author gky
 * @date 2023/08/26 11:34
 * @description
 */
@CrossOrigin
@RestController
@RequestMapping("/adviser")
public class AdviserController {
    @Autowired
    private AdviserService adviserService;

    /**
     * 分页查询指导老师
     *
     * @param currentPage 当前页码
     * @param pageSize    每页的大小
     * @return 指导老师列表
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public CommonResult<IPage<Adviser>> listAdviser(@PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        return adviserService.listAdviser(currentPage, pageSize);
    }

    /**
     * 根据id查询指导老师
     *
     * @param id 指导老师id
     * @return 单个指导老师
     */
    @GetMapping("/{id}")
    public CommonResult<Adviser> getById(@PathVariable Long id) {
        Adviser adviser = adviserService.getById(id);
        return new CommonResult<>(200, "success", adviser);
    }

    /**
     * 添加指导老师
     * @param adviser 指导老师
     * @return 成功与否
     */
    @PostMapping("/save")
    public CommonResult<String> save(@RequestBody Adviser adviser) {
        return adviserService.updateAdviser(adviser);
    }

    /**
     * 修改指导老师信息
     * @param adviser 指导老师信息
     * @return 成功与否
     */
    @PutMapping("/updateAdviser")
    public CommonResult<String> updateAdviser(@RequestBody Adviser adviser) {
        return adviserService.updateAdviser(adviser);
    }

    /**
     * 修改图片
     * @param id
     * @param photo
     * @return
     */
    @PutMapping("/updateImg/{id}")
    public CommonResult<String> updateImg(@PathVariable Long id, @RequestPart("photo") MultipartFile photo){
        return adviserService.updateImg(id,photo);
    }

    /**
     * 根据id删除指导老师
     *
     * @param id 指导老师id
     * @return 成功与否
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Long id) {
        if (adviserService.removeById(id)) {
            return new CommonResult<>(200, "success","恭喜你，删除成功！");
        } else {
            return new CommonResult<>(200, "error","删除失败");
        }
    }

}
